package com.example.demo.utils.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: cj
 * @create: 15-03-15 15:36:57
 */
public class RegexUtil {
    private static String regexRule = "^[1]\\d{10}$";
    private static Pattern pattern = Pattern.compile( regexRule );

    /**
     * 手机号验证
     *
     * @param phone 手机号
     * @return 是否是 手机号
     */
    public static boolean phoneRegex(String phone) {
        return pattern.matcher( phone ).matches();
    }

    /**
     * ip 校验
     *
     * @param ip ip
     * @return 是否是ip
     */
    public static boolean ipRegex(String ip) {
//        String regexRule = "/\\b(([01]?\\d?\\d|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d?\\d|2[0-4]\\d|25[0-5])\\b/";
        String regexRule = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";
        return Pattern.compile( regexRule ).matcher( ip ).matches();
    }

    /**
     * 把文本中的所有需要替换的变量捞出来, 丢进keys
     *
     * @return
     */
    public static Matcher getRepalse(String text) {
        String regexRule = "((?<=\\#\\{)([a-zA-Z_]{1,})(?=\\}))";
        pattern = Pattern.compile( regexRule );
        return pattern.matcher( text );
    }


}
